package com.znn.sp;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;



public class KJST {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String[] strings = new String[]{"1738","1847","1783","1954","1904","2556","2585","2610","2641","2706","2748","4121"};
		try {
			for (int ii = 0; ii < strings.length; ii++) {
				for (int i = 1; i <= 11; i++) {//11节课
					for (int j = 1; j <= 20; j++) {//20周
						for (int k = 1; k <= 7; k++) {//7天
							getKjsByBuildingAndClass("1709", strings[ii], i, j + "",
									k + "");
						}
					}
				}
			}
			
		}catch (KjsException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("OK!!!!!!!!!!!");
	}
	/**
	 * aid:
		1709 中心校区
		2312 西校区
		2348 中心校区北院
		2555 东校区东院
		2705 东校区西院<br/>
		* 中心校区buildingid:
	 * 2278 中心校区东院12教
	 * 2139 中心校区东院静思1教
	 * 2099 中心校区东院静思2教
	 * 2186 中心校区东院静思3教
	 * 1738 中心校区东院学思楼
	 * 1847 中心校区西院博文楼
	 * 1783 中心校区西院博学楼
	 * 1954 中心校区西院博远楼
	 * 1904 中心校区西院博知楼<br />
	 * 东校区buildingid:
	 * 2556 东校区东院1教
		2585 东校区东院2教
		2610 东校区东院3教
		2641 东校区东院4教
		2706 东校区西院1教
		2748 东校区西院2教
		4121 东校区西院图文中心
	 * @param j_username
	 * @param j_password
	 * @param buildingid
	 * @param clazz
	 * @return
	 * @throws IOException
	 * @throws ScoreException
	 * @throws KjsException
	 */
	public  static List<KJS> getKjsByBuildingAndClass(String aid,String buildingid, int clazz,String whichweek,String week) throws KjsException {
		//getQduScore("200940600064","501863b",2009,2);
		List<KJS> list = new ArrayList<KJS>();
		KJS kjs =null;
		String cookieString = "A9DECB5C1002E9D644A1FB62D8D48DD2.TAB2";
		String userAgent = "Mozilla/5.0 (Windows NT 6.1; rv:20.0) Gecko/20100101 Firefox/20.0";
		String personUrl = "http://jw.qdu.edu.cn/academic/teacher/teachresource/roomschedule_week.jsdo";
//		Map<String, String> map = Jsoup.connect("http://jw.qdu.edu.cn/academic/j_acegi_security_check")
//				.userAgent(userAgent)
//				.timeout(9*1000)
//				.data("j_username",j_username,"j_password",j_password)
//				.execute().cookies();
//		cookieString = map.get("JSESSIONID");
//		System.out.println("Cookie:"+cookieString);
		Map<String, String> map2 = new HashMap<String, String>();
		map2.put("aid", aid);
		map2.put("buildingid", buildingid);
		map2.put("room", "-1");
		map2.put("whichweek", whichweek);
		map2.put("week", week);
		Document document = null;
		try {
			document = Jsoup.connect(personUrl)
					.userAgent(userAgent)
					.cookie("JSESSIONID", cookieString)
					.data(map2)
					.referrer("http://jw.qdu.edu.cn/academic/teacher/teachresource/roomschedulequery.jsdo")
					.timeout(26*1000)
					.post();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (document == null) {
			return list;
		}
		if (document.title().contains("登录")) {
			throw new KjsException("过期");
		}
		if(document.toString().contains("请重新登录")){
			throw new KjsException("密码或用户名错误");
		}
		//System.out.println(document.toString());
		Elements listNodes = document.select("tr.infolist_common");
		if (listNodes.size() == 0) {
			//throw new KjsException("没有空教室");
		}
		for (int i = 0; i < listNodes.size(); i++) {
			Element listNode = listNodes.get(i);
			String string =listNode.text(); 
//			 System.out.println(string);
			Pattern pattern = Pattern.compile("(\\S+) (\\S+) (\\S+) (\\S+) (\\S+) (\\S+) 第1节 第2节 第3节 第4节 第5节 第6节 第7节 第8节 第9节 第10节 第11节 (\\S+) (\\S+) (\\S+) (\\S+) (\\S+) (\\S+) (\\S+) (\\S+) (\\S+) (\\S+) (\\S+)");
			Matcher matcher = pattern.matcher(string);
			
			if (matcher.matches()) {
				kjs = new KJS();
				kjs.setJsm(matcher.group(1));
				kjs.setZws(matcher.group(2));
				kjs.setPkrl(matcher.group(3));
				kjs.setKsrl(matcher.group(4));
				kjs.setZylx(matcher.group(5));
				kjs.setJslx(matcher.group(6));
				String[] iskong = new String[11];
				iskong[0]=matcher.group(7).replaceAll(Jsoup.parse("&nbsp;").text(), "1");;
				iskong[1]=matcher.group(8).replaceAll(Jsoup.parse("&nbsp;").text(), "1");;
				iskong[2]=matcher.group(9).replaceAll(Jsoup.parse("&nbsp;").text(), "1");;
				iskong[3]=matcher.group(10).replaceAll(Jsoup.parse("&nbsp;").text(), "1");;
				iskong[4]=matcher.group(11).replaceAll(Jsoup.parse("&nbsp;").text(), "1");;
				iskong[5]=matcher.group(12).replaceAll(Jsoup.parse("&nbsp;").text(), "1");;
				iskong[6]=matcher.group(13).replaceAll(Jsoup.parse("&nbsp;").text(), "1");;
				iskong[7]=matcher.group(14).replaceAll(Jsoup.parse("&nbsp;").text(), "1");;
				iskong[8]=matcher.group(15).replaceAll(Jsoup.parse("&nbsp;").text(), "1");;
				iskong[9]=matcher.group(16).replaceAll(Jsoup.parse("&nbsp;").text(), "1");;
				iskong[10]=matcher.group(17).replaceAll(Jsoup.parse("&nbsp;").text(), "1");;
				kjs.iskong = iskong;
				if (clazz<11&&iskong[clazz-1].equals("1")) {
					list.add(kjs);
				}
				
			}else {
				//throw new KjsException("解析失败(row)");
			}
		}
		DATA.writeToData(aid, buildingid, clazz+"", whichweek, week, list);
//		if((list == null || list.size()==0)&&listNodes.size()>0){
//			throw new KjsException("没有空教室！");
//		}
		return list;
	}
}

class KJS{
	String jsm;//教室名
	String zws;//座位数
	String pkrl;//排课容量容量
	String ksrl;//考试容量
	String zylx;//桌椅类型
	String jslx;//教室类型
	String[] iskong = new String[11];//sh是否是空教室 1是 0不是
	public String getJsm() {
		return jsm;
	}
	public void setJsm(String jsm) {
		this.jsm = jsm;
	}
	public String getZws() {
		return zws;
	}
	public void setZws(String zws) {
		this.zws = zws;
	}
	public String getKsrl() {
		return ksrl;
	}
	public void setKsrl(String ksrl) {
		this.ksrl = ksrl;
	}
	public String getZylx() {
		return zylx;
	}
	public void setZylx(String zylx) {
		this.zylx = zylx;
	}
	public String getJslx() {
		return jslx;
	}
	public void setJslx(String jslx) {
		this.jslx = jslx;
	}
	public String[] getIskong() {
		return iskong;
	}
	public void setIskong(String[] iskong) {
		this.iskong = iskong;
	}
	
	public String getPkrl() {
		return pkrl;
	}
	public void setPkrl(String pkrl) {
		this.pkrl = pkrl;
	}
	@Override
	public String toString() {
		return "KJS [jsm=" + jsm + ", zws=" + zws + ", pkrl=" + pkrl
				+ ", ksrl=" + ksrl + ", zylx=" + zylx + ", jslx=" + jslx
				+ ", iskong=" + Arrays.toString(iskong) + "]";
	}
	
	
}
